package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageBean;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.dto.NursingLevelPageQueryDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NursingLevelServiceImpl implements NursingLevelService {

    @Autowired
    NursingLevelMapper nursingLevelMapper;

    @Override
    public PageBean<NursingLevelVo> getNursinglevlsByPage(NursingLevelPageQueryDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        Page<NursingLevelVo> page = nursingLevelMapper.getNursinglevlsByPage(dto);
        return PageBean.of(page, NursingLevelVo.class);

    }

    @Override
    public void add(NursingLevelDto nursingLevelDto) {
        NursingLevel level = BeanUtil.toBean(nursingLevelDto, NursingLevel.class);


        level.setRemark(nursingLevelDto.getRemark());
        nursingLevelMapper.add(level);
    }

    @Override
    public NursingLevelVo getById(Long id) {

        return nursingLevelMapper.getById(id);
    }

    @Override
    public void update(NursingLevelDto nursingLevelDto) {

        NursingLevel level = BeanUtil.toBean(nursingLevelDto, NursingLevel.class);
        level.setRemark(nursingLevelDto.getRemark());
        nursingLevelMapper.update(level);
    }

    @Override
    public void delete(Long id) {
        if (nursingLevelMapper.getById(id).getStatus() == 1) {
            throw new RuntimeException("该护理等级已被使用，无法删除");
        }
        nursingLevelMapper.delete(id);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        NursingLevel level = NursingLevel.builder().id(id).status(status).build();
        nursingLevelMapper.update(level);
    }

    @Override
    public List<NursingLevel> getAll() {

        return nursingLevelMapper.getAll();
    }
}


